package com.yunfei.api.v2.qiuzhi.service.impl;

import com.yunfei.api.common.contants.ApiValitationConstantMessage;
import com.yunfei.api.v2.qiuzhi.contants.QiuZhiContants;
import com.yunfei.api.v2.qiuzhi.result.QiuZhiResult;
import com.yunfei.api.v2.qiuzhi.service.QiuZhiCenterService;
import com.yunfei.api.v2.user.valid.QiuZhiValid;
import com.yunfei.api.v2.user.valid.TokenForm;
import com.yunfei.bo.SkQuesDtlBo;
import com.yunfei.bo.TPersonalUserBo;
import com.yunfei.common.json.result.ResultModel;
import com.yunfei.core.utils.TokenUtil;
import com.yunfei.core.utils.enmu.ResultStatus;
import com.yunfei.core.utils.string.StringUtil;
import com.yunfei.entity.TPersonalUserEntity;
import com.yunfei.mapper.aiiidb.SkQuesDtlMapper;
import com.yunfei.mapper.aiiidb.TPersonalUserMapper;
import com.yunfei.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: zntg_v1
 * @description:
 * @author: zhengyunfei
 * @create: 2018-10-30 15:48
 **/
@Service("qiuZhiCenterService")
public class QiuZhiCenterServiceImpl implements QiuZhiCenterService {
    @Autowired
    private SkQuesDtlMapper skQuesDtlMapper;
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private RedisService redisService;
    /***校验参数*/
    public boolean ValidQiuZhiParam(BindingResult bindingResult, ResultModel resultModel) {
        boolean pageNowReg=false;
        boolean pageSizeReg=false;
        List<ObjectError> list = bindingResult.getAllErrors();
        for (ObjectError error : list) {
            String defaultMessage=error.getDefaultMessage();
            if(defaultMessage.equals(ApiValitationConstantMessage.PAGFE_NOW_NUM_REG)){
                pageNowReg=true;
            }
            else if(defaultMessage.equals(ApiValitationConstantMessage.PAGE_SIZE_NUM_REG)){
                pageSizeReg=true;
            }

        }
        if(pageNowReg){
            resultModel.setMessage(ResultStatus.PAGE_NOW_REG_ERROR.getMessage());
            resultModel.setCode(ResultStatus.PAGE_NOW_REG_ERROR.getCode());
            return true;
        }
        if(pageSizeReg){
            resultModel.setMessage(ResultStatus.PAGE_SIZE_REG_ERROR.getMessage());
            resultModel.setCode(ResultStatus.PAGE_SIZE_REG_ERROR.getCode());
            return true;
        }
        return false;
    }
    /**根据条件查询结果,用户求知广场查询**/
    public List<QiuZhiResult> getQiuZhiResults(int pageNow, int pageSize, SkQuesDtlBo skQuesDtlBo) {
        List<QiuZhiResult> result=null;
        List<SkQuesDtlBo> list=null;
        if(!StringUtil.isNullOrEmpty(pageNow)&&!StringUtil.isNullOrEmpty(pageSize)){
            skQuesDtlBo.setCurNo(pageNow);
            skQuesDtlBo.setCurSize(pageSize);
            list=skQuesDtlMapper.findAllListPage(skQuesDtlBo);
        }else {
            list=skQuesDtlMapper.findAllList(skQuesDtlBo);
        }
        if(!StringUtil.isNullOrEmpty(list)&&list.size()>0){
            result=new ArrayList<>();
            for(int i=0;i<list.size();i++){
                SkQuesDtlBo bo=list.get(i);
                String userId=bo.getUserId();
                TPersonalUserBo tPersonalUserEntity= tPersonalUserMapper.findById(userId);
                 if(!StringUtil.isNullOrEmpty(tPersonalUserEntity)){
                     bo.setUserName(tPersonalUserEntity.getUserName());
                     bo.setUserType(tPersonalUserEntity.getUserType());
                     bo.setHeadIcon(tPersonalUserEntity.getIconUrl());
                 }
                QiuZhiResult qiuZhiResult=SkQuesDtlBo.boToApiResult(bo);
                result.add(qiuZhiResult);
            }

        }
        return result;
    }
    /**
     * @Description: 获取最新求知广场信息
     * @return: boolean
     * @Author: zhengyunfei
     * @Date: 2018/11/1
     * @time:9:23
     */
    public boolean zuixin(@Valid QiuZhiValid requestParam, BindingResult bindingResult, ResultModel resultModel) {
        String quesTit=requestParam.getQuesTit();
        String pageNow=requestParam.getPageNow();
        int pageSize=20;
        int page=1;
        if(!StringUtil.isNullOrEmpty(pageNow)){
            page=Integer.parseInt(pageNow);
        }
        SkQuesDtlBo skQuesDtlBo=new SkQuesDtlBo();
        if(!StringUtil.isNullOrEmpty(quesTit)){
            skQuesDtlBo.setQuesTit(quesTit);
        }
        //默认只有审核通过的才展示，除了我的之外
        skQuesDtlBo.setCheckFlg(QiuZhiContants.CHECK_FLG_Y);
        int count=skQuesDtlMapper.count(skQuesDtlBo);
        if(count==0){
            resultModel.setCode(ResultStatus.NO_DATA.getCode());
            resultModel.setMessage(ResultStatus.NO_DATA.getMessage());
            return true;
        }
        List<QiuZhiResult> result = getQiuZhiResults((page-1)*pageSize, page*pageSize, skQuesDtlBo);
        if(!StringUtil.isNullOrEmpty(result)&&result.size()>0) {
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }else{
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.NO_MORE.getCode());
            resultModel.setMessage(ResultStatus.NO_MORE.getMessage());
        }
        return false;
    }
    /**
     * @Description: 求知广场推荐
     * @return: boolean
     * @Author: zhengyunfei
     * @Date: 2018/11/1
     * @time:9:28
     */
    public boolean tuijian(@Valid QiuZhiValid requestParam, BindingResult bindingResult, ResultModel resultModel) {
        String pageNow=requestParam.getPageNow();
        int pageSize=20;
        int page=1;
        if(!StringUtil.isNullOrEmpty(pageNow)){
            page=Integer.parseInt(pageNow);
        }
        String quesTit=requestParam.getQuesTit();
        SkQuesDtlBo skQuesDtlBo=new SkQuesDtlBo();
        if(!StringUtil.isNullOrEmpty(quesTit)){
            skQuesDtlBo.setQuesTit(quesTit);
        }
        //推荐(默认只要是推荐的，肯定是审核通过的)
        //默认只有审核通过的才展示，吃了我的之外
        skQuesDtlBo.setCheckFlg(QiuZhiContants.CHECK_FLG_Y);
        skQuesDtlBo.setRemdFlg(QiuZhiContants.REMD_FLG_Y);
        int count=skQuesDtlMapper.count(skQuesDtlBo);
        if(count==0){
            resultModel.setCode(ResultStatus.NO_DATA.getCode());
            resultModel.setMessage(ResultStatus.NO_DATA.getMessage());
            return true;
        }
        List<QiuZhiResult> result = getQiuZhiResults((page-1)*pageSize, page*pageSize, skQuesDtlBo);
        if(!StringUtil.isNullOrEmpty(result)&&result.size()>0) {
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }else{
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.NO_MORE.getCode());
            resultModel.setMessage(ResultStatus.NO_MORE.getMessage());
        }
        return false;
    }
    /**
     * @Description: 求知广场我的
     * @return: boolean
     * @Author: zhengyunfei
     * @Date: 2018/11/1
     * @time:9:34
     */
    public boolean wode(@Valid QiuZhiValid requestParam, BindingResult bindingResult, @Valid TokenForm tokenForm, ResultModel resultModel) {
        String pageNow=requestParam.getPageNow();
        int pageSize=20;
        int page=1;
        if(!StringUtil.isNullOrEmpty(pageNow)){
            page=Integer.parseInt(pageNow);
        }
        String quesTit=requestParam.getQuesTit();
        String token=tokenForm.getToken();

        if(StringUtil.isNullOrEmpty(token)){
            resultModel.setCode(ResultStatus.TOKEN_NOTNULL.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_NOTNULL.getMessage());
            return true;
        }/*else if(token.length()< 64){
            resultModel.setCode(ResultStatus.TOKEN_WUXIAO.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_WUXIAO.getMessage());
            return true;
        }*/
        if(redisService.exists(token)){
            SkQuesDtlBo skQuesDtlBo=new SkQuesDtlBo();
            if(!StringUtil.isNullOrEmpty(quesTit)){
                skQuesDtlBo.setQuesTit(quesTit);
            }
            //根据token获取用户userId
            String userId= TokenUtil.getInstance().getUserId(token);
            skQuesDtlBo.setUserId(userId);
            int count=skQuesDtlMapper.count(skQuesDtlBo);
            if(count==0){
                resultModel.setCode(ResultStatus.NO_DATA.getCode());
                resultModel.setMessage(ResultStatus.NO_DATA.getMessage());
                return true;
            }else{
                List<QiuZhiResult> result = getQiuZhiResults((page-1)*pageSize, page*pageSize, skQuesDtlBo);
                if(!StringUtil.isNullOrEmpty(result)&&result.size()>0) {
                    resultModel.setData(result);
                    resultModel.setCode(ResultStatus.SUCCESS.getCode());
                    resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                }else{
                    resultModel.setData(result);
                    resultModel.setCode(ResultStatus.NO_MORE.getCode());
                    resultModel.setMessage(ResultStatus.NO_MORE.getMessage());
                }
            }


        }else{
            resultModel.setCode(ResultStatus.TOKEN_ERROR.getCode());
            resultModel.setMessage(ResultStatus.TOKEN_ERROR.getMessage());
        }
        return false;
    }


}
